package com.smssalama;

import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.smssalama.gui.Login;
import com.smssalama.gui.setup.Start;
import com.smssalama.transport.SMSTransport;

/**
 * SMSSalama Midlet
 * 
 * @author Arnold P. Minde
 */
public class SMSSalama extends MIDlet {
	public static final int STATE_PAUSED = 0;
	public static final int STATE_ACTIVE = 1;
	public static final int STATE_DESTROYED = 2;

	static SMSSalama instance;
	private SMSTransport transport;
	private int state;
	private boolean isNew = true;

	public SMSSalama() {
		SMSSalama.instance = this;
		this.state = STATE_PAUSED;
	}

	public static SMSSalama getInstance() {
		return SMSSalama.instance;
	}

	public SMSTransport getTransport() {
		return this.transport;
	}

	public static void promptAuthentication(Displayable next) {
		Login login = new Login(next);
		AlertManager.showDisplayable(login);
	}

	private CommandListener setupCmdListener;

	public void startApp() throws MIDletStateChangeException {
		this.state = STATE_ACTIVE;
		if(!this.isNew){
			resumeApp();
			return;
		}
		this.isNew = false;
		SMSSalama.instance = this;
		try {
			//this.transport = Utils.initTransport(TransportBridge.getBridge());
			if(this.transport==null){
				this.transport = new SMSTransport(TransportBridge.getBridge());
				this.transport.open();
				TransportBridge.getBridge().setTransport(this.transport);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MIDletStateChangeException(
					"Failed to initialize transport: " + e.getMessage());
		}

		SMSSalama.ticker = new Ticker("SMS Salama");
		try {
			if (UserProfile.initialized()) {
				// go to login
				SMSSalama.promptAuthentication(null);
			} else {
				// go to setup
				Start start = new Start();
				AlertManager.showDisplayable(start);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new MIDletStateChangeException(ex.getMessage());
		}
	}
	

	public void pauseApp() {
		this.state = STATE_PAUSED;
	}
	
	protected void resumeApp(){
		//Shows the login dialog, which after successfull login will display the current displayable
		Displayable nextDisplayable = Display.getDisplay(this).getCurrent();
		promptAuthentication(nextDisplayable);
	}
	

	public void destroyApp(boolean unconditional) {
		this.state = STATE_DESTROYED;
		this.notifyPaused();
	}
	
	public static void quitApp() {
		SMSSalama.instance.destroyApp(true);
		SMSSalama.instance.notifyDestroyed();
		SMSSalama.instance = null;
	}
	
	public void pauseSMSSalama(){
		this.notifyPaused();
		pauseApp();
	}
	
	public void resumeSMSSalama(){
		this.resumeRequest();
	}

	public static Ticker ticker = null;
}
